<?php

/*
 * Controller/ContratTypeController.php
 */

namespace RHX\Controller;

class ContratTypeController extends RHXController {

    public function indexAction() {
	//
    }

    public function loadIndexAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// Tables
	$typeTable = $this->getService('Table\ContratTypeTable');

	// Liste des Types de Contrat
	$typeListe = $typeTable->getAll();

	return array(
	    'typeListe' => $typeListe,
	);
    }

    public function editAction() {
	// changement de layout
	$this->layout('layout/ajax');

	// POST
	$post = $this->getRequest()
		->getPost();

	// Adapter
	$adapter = $this->getAdapter();

	// Variables
	$pk	 = $post->pk;
	$name	 = $post->name;
	$value	 = addslashes($post->value);
	if ($name == 'coef') {
	    $value = str_replace(',', '.', $value);
	}

	// Requete SQL
	$sql	 = "UPDATE rh_contrat_type SET $name = '$value' WHERE id = $pk";
	$result	 = $adapter->query($sql)
		->execute();

	return array(
	    'sql'	 => $sql,
	    'result' => $result,
	);
    }

    public function addAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// table
	$contratTypeTable = new \RHX\Model\Table\ContratTypeTable($this->getAdapter());

	// Post
	$post = $this->getRequest()
		->getPost();

	// Nouveau type de contrat
	$type = new \RHX\Model\Object\ContratType($post);

	// Enregistrement dans la base de données
	$id		 = $contratTypeTable->save($type);
	$type->id	 = $id;

	return array(
	    'type' => $type,
	);
    }

    /**
     *
     * @return \Zend\View\Model\JsonModel
     */
    public function deleteAction() {
	// changement de layout
	$this->layout('layout/ajax');
	//post
	$post		 = $this->getRequest()
		->getPost();
	// Table ContratType
	$tableType	 = new \RHX\Model\Table\ContratTypeTable($this->getAdapter());
	$nb		 = $tableType->erase($post->id);

	return new \Zend\View\Model\JsonModel(array(
	    'nb'	 => $nb,
	    'done'	 => ($nb > 0 ) ? true : false,
	));
    }

}

?>
